package blacknWhite.MarketSalesMonitor;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
import blacknWhite.Libraries.AppDatabase;
import blacknWhite.Libraries.Utils;
import blacknWhite.Libraries.AppDatabase.AppCommentsColumns;

public class CommentsDataAdapter extends ArrayAdapter<View> {

	LayoutInflater inflater;
	Context context;

	public CommentsDataAdapter(Context context) {
		super(context, R.layout.comments_item);

		this.context = context;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		refreshData();
	}

	private void refreshData() {
		try {
			AppDatabase mOpenHelper = null;
			SQLiteDatabase db = null;

			mOpenHelper = new AppDatabase(context);
			db = mOpenHelper.getReadableDatabase();
			if (cursor != null)
				cursor.close();
			cursor = db.query(AppDatabase.Tables.APP_COMMENTS, null, null,
					null, null, null, "creation_time DESC");
			((Activity) context).startManagingCursor(cursor);
		} catch (Throwable e) {
			Utils.LogException(e);
		}
	}

	Cursor cursor;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#finalize()
	 */
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		if (cursor != null)
			cursor.close();
		super.finalize();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (cursor == null)
			return 0;
		return cursor.getCount();
	}

	@Override
	public void notifyDataSetChanged() {
		// TODO Auto-generated method stub
		super.notifyDataSetChanged();
		refreshData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View row = null;
		try {
			cursor.moveToPosition(position);
			row = inflater.inflate(R.layout.comments_item, null);

			final TextView EditTextPachageId = (TextView) row
					.findViewById(R.id.EditTextPachageId);
			final RatingBar RatingBar01 = (RatingBar) row
					.findViewById(R.id.RatingBar01);
			final TextView EditTextCreationTime = (TextView) row
					.findViewById(R.id.EditTextCreationTime);
			final TextView EditTextAuthorName = (TextView) row
					.findViewById(R.id.EditTextAuthorName);
			final TextView EditTextComment = (TextView) row
					.findViewById(R.id.EditTextComment);

			String packageId = cursor
					.getString(AppCommentsColumns.PACKAGE_ID_ID);
			if (packageId == null)
				packageId = "";
			EditTextPachageId.setText(packageId);

			RatingBar01
					.setRating(cursor.getFloat(AppCommentsColumns.RATING_ID));
			RatingBar01.setClickable(false);
			RatingBar01.setEnabled(false);

			Long creationTimeLong = cursor
					.getLong(AppCommentsColumns.CREATION_TIME_ID);
			Date creationTime = new Date(creationTimeLong);
			java.text.DateFormat dateFormat = java.text.DateFormat
					.getDateTimeInstance();
			EditTextCreationTime.setText(dateFormat.format(creationTime));

			String authorName = cursor
					.getString(AppCommentsColumns.AUTHOR_NAME_ID);
			if (authorName == null)
				authorName = "";
			EditTextAuthorName.setText(authorName);

			String comment = cursor.getString(AppCommentsColumns.TEXT_ID);
			if (comment == null)
				comment = "";
			EditTextComment.setText(comment);

		} catch (Throwable e) {
			Utils.LogException(e);
		}

		if (row != null)
			return row;
		return super.getView(position, convertView, parent);
	}

}
